require("SOD/tools/dialogs/dialog.rb")
require("SOD/ProjectManager.rb")

module SOD
  
  class ProjectInfoDialog < SOD_Dialog # see OS Dialog
    
    def initialize(interface, title = "Default", scrollbars = true, w = 660, h = 580, l = 150, t = 150)
      super
      set_file("projectInfo.html")
      add_callbacks
    end
    
    def add_callbacks
      super
      @webDialog.add_action_callback("showVolumes") do |d, p|
	Plugin.model.drawVolumes
      end
    end
    
    def update
      
      get_selected_bar
      
      if @sodBar.nil?
        set_file("NoSelection.html")
      else
        
        puts "Dialog of " + @sodBar.name.to_s + " selected!" if $debug
        set_file("projectInfo.html")
        
        floorDesignArray = Plugin.designRules.floorDesign
        flexibilityArray = Plugin.designRules.flexibility
        storeyArray = Plugin.designRules.storeyList
        
        @projectName = Plugin.projectManager.activeProjectName
        @projectName = "DebugProject" if $debug
        @roomsize = @sodBar.barInfo.roomsize
        @flexibility = @sodBar.barInfo.flexibility
        @storeys = @sodBar.barInfo.numberOfStoreys.to_s
        
        @length = @sodBar.sketchInfo.footprintLength.to_s
        @width = @sodBar.sketchInfo.footprintWidth.to_s
        @origin = @sodBar.sketchInfo.pointOfOrigin.to_s
        @direction = @sodBar.sketchInfo.direction.to_s
        
        if @projectName.nil?
          @projectName = "No Project Active!"
        else
          set_select_options("ROOMSIZE", floorDesignArray)
          set_select_options("FLEXIBILITY", flexibilityArray)
          set_select_options("STOREYS", storeyArray)
          
          set_element_value("ROOMSIZE", @roomsize)
          set_element_value("FLEXIBILITY", @flexibility)
          set_element_value("STOREYS", @storeys)
        end
        
        set_element_value("NAME", @projectName)
        
        set_element_value( "LENGTH", @length )
        set_element_value( "WIDTH", @width )
        set_element_value( "ORIGIN", @origin )
        set_element_value( "DIRECTION", @direction )
        
      end
      
    end
    
    def on_change_element(d, p)
      # problem here:  assumes value does not have any commas in it, need to just split off first one
      args = p.split(",")
      key = args[0]
      value = args[1]
      
      case(key)
      when "ROOMSIZE"
        @sodBar.barInfo.roomsize = value
      when "FLEXIBILITY"
        @sodBar.barInfo.flexibility = value
      when "STOREYS"
        @sodBar.barInfo.numberOfStoreys = value.to_i
      else
        puts "ProjectInfo: No matching Value returned!" if $debug
      end
      
      if Plugin.dialogManager.active_interface(StructureGeneratorInterface)
        Plugin.dialogManager.update(StructureGeneratorInterface)
      end
      
    end
    
  end
  
end
